package column

import (
	"game/game-db/orm/metag"
	"reflect"
	"unsafe"
)

type IColumnMeta interface {
	FieldName() string       //结构体中的字段名
	FieldKind() reflect.Kind //字段的类别
	FieldType() string       //字段的类型
	ColumnName() string      //数据库中的字段名
	ReadOnly() bool          //是否自读
	Comment() string         //字段的备注

	DefaultValue() string
	FillValue(objField reflect.Value, fieldValue reflect.Value) //填充字段的值
	TakeValue(objField reflect.Value) interface{}               //获取字段的值
	DbColumnType() string
	IsChange(dbColumnType string, dbColumnExtra string) bool //列的定义是否发生的改变
}
type ColumnMeta struct {
	fieldName    string       //结构体中的字段名
	fieldKind    reflect.Kind //
	fieldType    string       //
	columnName   string       //数据表中的列名
	readOnly     bool         //是否只读
	comment      string       //备注
	defaultValue string       //默认值
}

func (t *ColumnMeta) fillBaseValue(structField reflect.StructField, columnMetag metag.ColumnMetag) {
	t.fieldName = structField.Name
	t.fieldKind = structField.Type.Kind()
	t.fieldType = structField.Type.Name()
	t.columnName = columnMetag.Name
	if t.columnName == "" {
		t.columnName = metag.MakeSnakeCase(structField.Name)
	}
	t.comment = columnMetag.Comment
	t.readOnly = columnMetag.ReadOnly
}

func (t *ColumnMeta) FieldName() string {
	return t.fieldName
}
func (t *ColumnMeta) FieldKind() reflect.Kind {
	return t.fieldKind
}
func (t *ColumnMeta) FieldType() string {
	return t.fieldType
}
func (t *ColumnMeta) ColumnName() string {
	return t.columnName
}
func (t *ColumnMeta) ReadOnly() bool {
	return t.readOnly
}
func (t *ColumnMeta) Comment() string {
	return t.comment
}

func setValue(objField reflect.Value, objValue reflect.Value) {
	if objField.CanSet() {
		objField.Set(objValue)
	} else {
		objField = reflect.NewAt(objField.Type(), unsafe.Pointer(objField.UnsafeAddr())).Elem()
		objField.Set(objValue)
	}
}
